let nums = [-1,2,1,-4], target = 1
var threeSumClosest = function (nums, target) {
    let newNum = nums.sort((a,b)=>a-b)
    /* console.log(newNum); */
    
    let result = 1000
    for(let i=0;i<newNum.length-2;i++){
        let left = i+1
        let right = newNum.length-1
        while(left<right){
                result = Math.abs((newNum[left]+newNum[right]+newNum[i]-target))<Math.abs(result-target)?(newNum[left]+newNum[right]+newNum[i]):result
/*                             console.log(i,left,right);
                            console.log(newNum[left]+newNum[right]+newNum[i]); */
                            
                if(newNum[left]+newNum[right]+newNum[i]>target){
                    right--
                }else{
                    left++
                }
        }
    }
    return result
};
console.log(threeSumClosest(nums,target));

